package code.c03_stackQueue.linkStack;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/2 21:41
 * @package code.c03_stackQueue.linkStack
 * @description 链栈 泛型类
 */

public class LinkStackClass<E> {
    // 存放头结点
    LinkNode<E> head;

    // 构造方法
    public LinkStackClass() {
        // 创建头结点
        head = new LinkNode<E>();
        // 设置空栈
        head.next = null;
    }

    // ================================ 顺序表的基本运算 ================================

    /**
     * 判断栈是否为空
     * @return
     */
    public boolean empty(){
        return head.next == null;
    }

    /**
     * 入栈操作：头插法
     * @param e
     */
    public void push(E e){
        LinkNode<E> s = new LinkNode<>(e);
        s.next = head.next;
        head.next = s;
    }

    public E pop(){
        if (empty()){
            throw new IllegalArgumentException("出栈：栈空");
        }
        E e = (E)head.next.data;
        head.next = head.next.next;
        return e;
    }

    public E peek(){
        if (empty()){
            throw new IllegalArgumentException("出栈：栈空");
        }
        return (E)head.next.data;
    }


}
